<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('消息管理列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>手机号码：</label>
                                <input type="text" name="mobilePhone"/>
                            </li>
                            <li>
                                <label>用户姓名：</label>
                                <input type="text" name="userName"/>
                            </li>
                            <li>
                                <label>消息类型：</label>
                                <select name="messageType">
                                    <option value="">所有</option>
                                    <option value="0">系统消息</option>
                                    <option value="1">短信消息</option>
                                    <option value="2">微信消息</option>
                                </select>
                            </li>
                            <li>
                                <label>状态：</label>
                                <select name="status">
                                    <option value="">所有</option>
                                    <option value="0">待发送</option>
                                    <option value="1">已发送</option>
                                    <option value="2">发送失败</option>
                                </select>
                            </li>
                            <li class="select-time">
                                <label>发送时间：</label>
                                <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="params[beginTime]"/>
                                <span>-</span>
                                <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="params[endTime]"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="business:message:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="business:message:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="business:message:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="business:message:export">
                    <i class="fa fa-download"></i> 导出
                </a>
                <a class="btn btn-info multiple disabled" onclick="batchSend()" shiro:hasPermission="business:message:edit">
                    <i class="fa fa-send"></i> 批量发送
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('business:message:edit')}]];
        var removeFlag = [[${@permission.hasPermi('business:message:remove')}]];
        var detailFlag = [[${@permission.hasPermi('business:message:detail')}]];
        var messageTypeData = [
            {code: "0", text: "系统消息"},
            {code: "1", text: "短信消息"},
            {code: "2", text: "微信消息"}
        ];
        var statusData = [
            {code: "0", text: "待发送"},
            {code: "1", text: "已发送"},
            {code: "2", text: "发送失败"}
        ];
        
        var prefix = ctx + "business/message";
        
        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "消息",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'messageId',
                    title: 'ID'
                },
                {
                    field: 'mobilePhone',
                    title: '手机号码'
                },
                {
                    field: 'userName',
                    title: '用户姓名'
                },
                {
                    field: 'messageType',
                    title: '消息类型',
                    formatter: function(value, row, index) {
                        for(var i=0; i<messageTypeData.length; i++) {
                            if(value == messageTypeData[i].code) {
                                return messageTypeData[i].text;
                            }
                        }
                        return value;
                    }
                },
                {
                    field: 'content',
                    title: '消息内容',
                    formatter: function(value, row, index) {
                        if (value && value.length > 20) {
                            return '<span title="' + value + '">' + value.substring(0, 20) + '...</span>';
                        }
                        return value;
                    }
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function(value, row, index) {
                        for(var i=0; i<statusData.length; i++) {
                            if(value == statusData[i].code) {
                                var statusClass = "";
                                if(value == "0") statusClass = "primary"; // 蓝色
                                else if(value == "1") statusClass = "success"; // 绿色
                                else if(value == "2") statusClass = "danger"; // 红色
                                
                                return '<span class="label label-'+statusClass+'">'+statusData[i].text+'</span>';
                            }
                        }
                        return value;
                    }
                },
                {
                    field: 'sendTime',
                    title: '发送时间'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        if (row.status == '0') {
                            actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="sendMessage(\'' + row.messageId + '\')"><i class="fa fa-send"></i> 发送</a> ');
                        }
                        if (editFlag) {
                            actions.push('<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.messageId + '\')"><i class="fa fa-edit"></i> 编辑</a> ');
                        }
                        if (detailFlag) {
                            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="$.operate.detail(\'' + row.messageId + '\')"><i class="fa fa-search"></i> 详情</a> ');
                        }
                        if (removeFlag) {
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.messageId + '\')"><i class="fa fa-trash"></i> 删除</a>');
                        }
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });
        
        /* 发送消息 */
        function sendMessage(messageId) {
            $.modal.confirm("确认要发送该消息吗？", function() {
                var url = prefix + "/send/" + messageId;
                $.operate.post(url);
            });
        }
        
        /* 批量发送消息 */
        function batchSend() {
            var rows = $.table.selectColumns("messageId");
            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            $.modal.confirm("确认要批量发送选中的" + rows.length + "条消息吗？", function() {
                var url = prefix + "/batchSend";
                var data = { "ids": rows.join() };
                $.operate.post(url, data);
            });
        }
    </script>
</body>
</html> 